home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1152 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: pwolf-mac.qualcomm.com!user
  2. From: pwolf@qualcomm.com (Paul I. Wolf )
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Is this somekind of a stack fault or what?
  5. Date: 11 Jan 1996 19:24:47 GMT
  6. Organization: Qualcomm, Inc.
  7. Message-ID: <pwolf-1101961124440001@pwolf-mac.qualcomm.com>
  8. References: <4d2oi6$ghf@gate.compart.fi>
  9. NNTP-Posting-Host: pwolf-mac.qualcomm.com
  10.  
  11. In article <4d2oi6$ghf@gate.compart.fi>, joonas.kervinen@pcb.compart.fi
  12. (joonas kervinen) wrote:
  13.  
  14. > I have a function that causes a bizarre problem. It's used in a
  15. > Windows program and a DOS version compiled with BCC 4.53 large model.
  16. > Everything is just straight C. I'm pretty sure this function is the
  17. > culprit since slight modifications to it (as described in the source)
  18. > make everything go all right.
  19. > The function purpose is to round() a number to certain decimal
  20. > accuracy. It is passed the number as a char pointer (char *sa2) and
  21. > the desired decimal count (deci). While the function is not foolproof
  22. > (it won't handle negative decimals or other clear errors) it is
  23. > sufficient for me. I've found out that sprintf() are not reliable in
  24. > rounding up situation (do a sprintf(mystr,"%f.2d",1.499) or something
  25. > like that and you'll get errors)
  26. > I can't figure why the func bombs stack or whatever but it seems to
  27. > overwrite memory or unbalance the stack. When I do a
  28. > roundx("  9.99 ",1)
  29. > I do get a pointer to "10.0". The problem is that after that strange
  30. > things happen to the rest of my program. Not a big crash but some
  31. > memory or pointer go away. I've carefully examined the func under
  32. > Turbo debugger and it just doesn't overwrite memory. I tend to think
  33. > that there is something wrong with this:
  34. > char *roundx(char *sa2, int deci)
  35. > {
  36. > /* */
  37. > static char sa1="                   ";
  38. > /* rest of program */
  39. > return sa1;
  40. > }
  41. > When I change it to:
  42. > char *roundx(char *sa2, int deci)
  43. > {
  44. > /* */
  45. > char sa1[20];
  46. > /* rest of program with one modification*/
  47. > return sa1;
  48. > }
  49. > ... everything works on DOS and Windows version.
  50. > Please tell me what's going on. Please answer via E-mail since I don't
  51. > come to comp.lang.c often (this is also on Windows programming
  52. > section)
  53.  
  54. I didn't inspect your actual function logic, bu try changing your
  55. declaration to either one of:
  56.  
  57. static char* sa1    = "                   ";
  58. static char  sa1 [] = "                   ";
  59.  
  60. Your compiler should have produced some warning about the return value.
  61.